#/bin/bash -e

declare -i block_start=50
declare -i block_end=1800
declare -i step=50
time_count=0

peer_name=`docker ps | grep fabric-peer | awk '{print $NF}'`
sudo cat `docker inspect --format='{{.LogPath}}' $peer_name` | grep "with 500 transaction(s)" > ~/fabric_validate.log

grep "block \[$block_start\]" ~/fabric_validate.log > ~/fabric_validate_res.log
time1=`grep "block \[$block_start\]" ~/fabric_validate.log | sed -n 's/.*"time":"\(.*\)T\(.*\)Z.*/\1 \2/p'`
timeStamp1=$(date --date="$time1" +%s.%N)
let block_start+=step

rm -f $HOME/tps.res

while (( block_start<=block_end ))
do
    grep "block \[$block_start\]" ~/fabric_validate.log > ~/fabric_validate_res.log
    time2=`grep "block \[$block_start\]" ~/fabric_validate.log | sed -n 's/.*"time":"\(.*\)T\(.*\)Z.*/\1 \2/p'`
    timeStamp2=$(date --date="$time2" +%s.%N)
    delta_time=$(echo "$timeStamp2-$timeStamp1" | bc)
    time_count=$(echo "$time_count+$delta_time" | bc)
    echo -n "$time_count " >> $HOME/tps.res
    echo "500*$step/$delta_time" | bc >> $HOME/tps.res
    timeStamp1=$timeStamp2
    let block_start+=step
done

rm ~/fabric_validate.log ~/fabric_validate_res.log
